<?php

namespace app\api\service\orderanswer;

use app\api\service\Basics;
use think\Db;

/** 微信支付问答订单后的回调 */
class PaySuccess extends Basics
{
    /** 构造函数，获取订单信息 */
    public $orderObjArr;
    public function __construct($orderNo)
    {
        // 实例化订单模型
        $this->orderObjArr = Db::name('qa_buyorder')->where('ord_order',$orderNo)->find();
    }

    /** 回调方法 */
    public function onPaySuccess($payType, $signData = [])
    {
        // 更新问答订单状态
        if($this->orderObjArr['ord_status'] == 1){
            $ordId = $this->orderObjArr['ord_id'];
            $arr['ord_status'] = 8;
            $arr['ord_paytime_str'] = date('Y-m-d H:i:s', time());
            $isOk = Db::name('qa_buyorder')->where('ord_id',$ordId)->update($arr);
            $this->orderObjArr['ord_status'] = 8;

            // 1. 购买成功后，将购买到的哪些试卷入库
            $qaBoxArr = $this->myQaIndb();
            // 2. 购买成功后，修改余额，钱包记录消费记录
            $all_pay_money = $this->orderObjArr['ord_need_money'];
            $yu_pay_money = $this->orderObjArr['ord_yu_money'];
            $wx_pay_money = $this->orderObjArr['ord_wx_money'];
            $userId = $this->orderObjArr['ord_uid'];
            $sql = "UPDATE ".getTable('user')." set balance=balance-$yu_pay_money , pay_money=pay_money+$all_pay_money where user_id=".$userId;
            $res = Db::query($sql);
            // 3. 购买成功后，增加试卷售出的数量
            //print_r($this->orderObjArr);
            $qaIdsStr = trim($this->orderObjArr['ord_qa_ids']);
            $sql22 = "UPDATE ".getTable('qa_boxquestion')." set bq_sellnum_true=bq_sellnum_true+1 where bq_id in ($qaIdsStr)";
            $res22 = Db::query($sql22);


            $inArr['user_id'] = $userId;
            $inArr['scene'] = 20;
            $inArr['money'] = -$all_pay_money;
            $inArr['pay_type'] = $wx_pay_money>0 ? 2 : 0;
            $inArr['describe'] = "用户购买问答，订单号：".$this->orderObjArr['ord_order'];
            $inArr['remark'] = "余额支付 $yu_pay_money 元，微信支付 $wx_pay_money 元";
            $inArr['log_payid'] = $this->orderObjArr['ord_id'];
            $inArr['wxapp_id'] = $this->orderObjArr['wxapp_id'];
            $inArr['create_time'] = time();
            $inArr['create_timestr'] = date('Y-m-d H:i:s', time());
            Db::name('user_balance_log')->insert($inArr);

            // 3. 给购买人的直推和间推返佣，返佣后甚至直接给金额到账
            $needFenMoney = 0;
            foreach ($qaBoxArr as $key => $value){
                $bq_down_money = $value['bq_down_money'];
                if($bq_down_money > 0){
                    $needFenMoney += $bq_down_money*0.8;
                }
            }
            $this->qaFenXiao($this->orderObjArr, $needFenMoney);
        }
        return true;
    }

    /** 根据用户购买的哪些试卷，将这些试卷入库 */
    private function myQaIndb(){
        $backArr = array();
        $orderData = $this->orderObjArr;
        $qaIdsStr = trim($orderData['ord_qa_ids']);
        if($qaIdsStr!=""){
            $qaIdArr = explode(',', $qaIdsStr);
            $quBoxList = Db::name('qa_boxquestion')->whereIn('bq_id', $qaIdArr)->select()->toArray();
            foreach ($qaIdArr as $key => $value){
                $inArr['buy_uid'] = $orderData['ord_uid'];
                $inArr['buy_unick'] = $orderData['ord_unick'];
                $inArr['buy_uheader'] = $orderData['ord_uheader'];
                $inArr['buy_qa_id'] = $value;
                $qaObj = $this->findQaObj($quBoxList, $value);
                $backArr[] = $qaObj;
                $inArr['buy_qa_title'] = $qaObj['bq_title'];
                $inArr['buy_qa_price'] = $qaObj['bq_price'];
                $inArr['buy_alljifen'] = $qaObj['bq_perjifen'] * $qaObj['bq_num'];
                $inArr['buy_qa_des'] = $qaObj['bq_des'];
                $inArr['buy_easy'] = $qaObj['bq_easy'];
                $inArr['buy_qa_num'] = $qaObj['bq_num'];
                $inArr['wxapp_id'] = $orderData['wxapp_id'];
                $inArr['buy_addtime_str'] = date('Y-m-d H:i:s',time());
                Db::name('qa_buyquestion')->insert($inArr);
            }
        }
        return $backArr;
    }
    /** 通过试卷id，查找试卷数据 */
    private function findQaObj($quBoxList, $id){
        $obj = [];
        foreach ($quBoxList as $value){
            if($value['bq_id']==$id){
                $obj = $value;
                break;
            }
        }
        return $obj;
    }

    /** 根据试卷是否返佣，实时返给用户推荐费 */
    private function qaFenXiao($orderObj, $needFenMoney){
        if($needFenMoney > 0.2){
            $uid = $orderObj['ord_uid'];
            $fatherUid = 0;
        }
    }

    /** 获取订单信息 */
    public function getOrderInfo(){
        return $this->orderObjArr;
    }

    /** 相关错误信息 */
    public function getError()
    {
        return '问答订单回调出错';
    }
}